package com.yonyou.ucf.mdf.app.isvdxq.other.service;

import com.yonyou.ucf.mdf.app.isvdxq.other.mapper.impl.ProductSKUMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.yonyisv.model.EventContent;
import com.yonyou.ucf.mdf.app.isvdxq.other.entity.ProductSKUEntity;
import com.yonyou.ucf.mdf.app.isvdxq.other.model.ContentBean;
import com.yonyou.ucf.mdf.app.isvdxq.other.model.ProductSKUBean;
import com.yonyou.ucf.mdf.app.isvdxq.utils.RequestTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class ProductSKUService {
    @Autowired
    private ProductSKUMapperImpl productSKUMapper;

    public ProductSKUEntity SelectById(Long Id) {
        return productSKUMapper.selectById(Id);
    }

    public Boolean CheckEntity(Long Id) {
        int rInt = productSKUMapper.selectCount(Id);
        if (rInt > 0) return true;
        else return false;
    }

    public Integer InsertEntity(ProductSKUEntity entity) {
        return productSKUMapper.insert(entity);
    }

    public Integer UpdateEntity(ProductSKUEntity entity) {
        return productSKUMapper.updateByPrimaryKey(entity);
    }

    public List<ProductSKUEntity> SelectAll(String tenantId) {
        return productSKUMapper.selectAll(tenantId);
    }

    public Integer DeleteById(Long Id) {
        return productSKUMapper.deleteByPrimaryKey(Id);
    }

    public Integer EventEdit(EventContent content) throws IOException {
        System.out.println(content.getContent());
        ContentBean obj = RequestTool.getBean(content.getContent(), ContentBean.class);
        String jsonString = obj.getArchive().replace("detail!id", "detailid").replace("detail!", "");
        ProductSKUBean productSKUBean = RequestTool.getBean(jsonString, ProductSKUBean.class);
        String itemName = productSKUBean.getShowItem().toString();
        String aliasName = productSKUBean.getUserdefAlias().toString();
        ProductSKUEntity productSKUEntity = RequestTool.getBean(jsonString, ProductSKUEntity.class);
        productSKUEntity.setShowItemName(itemName);
        productSKUEntity.setUserdefAliasName(aliasName);
        productSKUEntity.setTenant_id(obj.getTenantId());
        Boolean flag = CheckEntity(productSKUEntity.getId());
        if (flag) {
            int rInt = UpdateEntity(productSKUEntity);
            return rInt;
        } else {
            int rInt = InsertEntity(productSKUEntity);
            return rInt;
        }
    }

    public Integer EventDel(EventContent content) throws IOException {
        System.out.println(content.getContent());
        ContentBean obj = RequestTool.getBean(content.getContent(), ContentBean.class);
        Long Id = Long.parseLong(obj.getId());
        Boolean flag = CheckEntity(Id);
        if (flag) {
            int rInt = DeleteById(Id);
            return rInt;
        }
        return 0;
    }
}
